<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field['options'].displayStyle"
  >
    <div class="scene-public-board" :style="getStyle('area')">
      <div class="board-wrap">
        <div class="board-inside flex-box">
          <template v-for="(patientItem,index) in getPatientListInfo('list', null)">
            <div class="flex-item" :class="{'flex-box-left':index===0,'flex-box-right':index===1}">
              <div class="header-area" :style="getStyle('header')">
                <template v-for="(item) in (optionsInfo['list']['colOptions'])">
                  <div class="header-item item" :class="item.className" :style="setFlex(item,'head')">
                    {{ item.label }}
                  </div>
                </template>
              </div>

              <div class="list-area">
                <div class="list-info" v-for="(item) in patientItem">
                  <template v-if="true" v-for="(items) in optionsInfo['list']['colOptions']">
                    <div class="list-item" :class="items.className" :style="setFlex(items,'list')">
                      <template v-if="items.value==='sex'">
                        <template v-if="item['age']&&item['sex'] ">
                          {{ item['sex'] || '\u00a0' }}-{{ item['age'] || '\u00a0' }}
                        </template>
                      </template>

                      <template v-else-if="items.value==='icon'">
                        <i class="el-icon-star-on sign" v-if="parseInt(item['icon'])===1"/>
                        <SvgIcon icon-class="circular" class-name="color-svg-icon" v-if="parseInt(item['icon'])===2"/>
                      </template>

                      <template v-else>
                        <template v-if="items.value==='bedNo'">
                            <span class="number"
                                  v-if="!!item&&!!item['bedNo']"
                                  :class="item['className']">
                              {{ item['bedNo'] || '\u00a0' }}
                            </span>
                        </template>
                        <template v-else>
                          {{ item[items['value']] || '\u00a0' }}
                        </template>
                      </template>

                    </div>
                  </template>

                </div>
              </div>

            </div>
          </template>
        </div>
      </div>
      <div class="total-area">

        <p class="total-item">
          急内&nbsp;&nbsp;:&nbsp;&nbsp;
          {{ getPatientListInfo('total', '急内') }}
        </p>
        <p class="total-item">
          急外&nbsp;&nbsp;:&nbsp;&nbsp;
          {{ getPatientListInfo('total', '急外') }}
        </p>
        <p class="total-item">
          神内&nbsp;&nbsp;:&nbsp;&nbsp;
          {{ getPatientListInfo('total', '神内') }}
        </p>
        <p class="total-item">
          总数&nbsp;&nbsp;:&nbsp;&nbsp;
          {{ getPatientListInfo('total', '总数') }}
        </p>
        <p class="total-item">
          <i class="el-icon-star-on sign"/>&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;
          {{ getPatientListInfo('total', '急内') }}
        </p>
        <p class="total-item">
          <SvgIcon icon-class="circular" class-name="color-svg-icon"/>&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;
          {{ getPatientListInfo('total', '急外') }}
        </p>
      </div>
    </div>

  </CompOperate>
</template>

<script>
import {v4 as uuid} from "uuid";
import {pxToVw} from "@/utils/common";
import emitter from "@/utils/designer/emitter";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import ScrollX from "@c/public/seamlessScroll/ScrollX.vue";
import SvgIcon from "@c/public/svg-icon/index.vue";
import {bizFields, containers, customFields, sceneFields} from "@c/public/form-designer/widgetsConfig";

export default {
  name: "scene-public-board-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    SvgIcon,
    ScrollX,
    CompOperate,
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */
      type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */
      type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
  },

  data() {
    return {
      optionsInfo: Array(),
    }
  },


  computed: {
    //生成唯一的字符串
    getOnlyId() {
      return function () {
        return uuid()
      }
    },

    // 模板的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Object();
        let Obj = Object();
        switch (val) {
          case "area":
            Reflect.set(Obj, "height", pxToVw(info.areaHeight));
            return Obj;
          case "header":
            let header = (info.header);
            Reflect.set(Obj, "color", (header.color));
            Reflect.set(Obj, "fontSize", pxToVw(header.fontSize));
            Reflect.set(Obj, "fontWeight", (header.fontWeight));
            Reflect.set(Obj, "background", (header.background));
            return Obj;
          case "list":
            let list = (info.list);
            Reflect.set(Obj, "color", (list.color));
            Reflect.set(Obj, "fontSize", pxToVw(list.fontSize));
            Reflect.set(Obj, "fontWeight", (list.fontWeight));
            return Obj;
        }
      };
    },

    // 设置宽度和字号
    setFlex() {
      return function (item, val) {
        const self = this;
        let info = (self.optionsInfo) || Object();
        let Obj = Object();
        switch (val) {
          case "head":
            Reflect.set(Obj, "flex", (item.flex) || 1);
            return Obj;
          case "list":
            let list = (info.list);
            Reflect.set(Obj, "color", (list.color));
            Reflect.set(Obj, "fontSize", pxToVw(list.fontSize));
            Reflect.set(Obj, "fontWeight", (list.fontWeight));
            Reflect.set(Obj, "flex", (item.flex) || 1);
            return Obj;
        }
      }
    },


    //获取患者信息
    getPatientListInfo() {
      return function (type, info) {
        let list;
        let devType = (this.devType);
        let boardPatientInfo = this.$store.getters['getBoardPatientInfo'];
        let {patientListTotal} = this.optionsInfo;
        if (devType) {
          list = boardPatientInfo['apiData']
        } else {
          list = boardPatientInfo['mockData']
        }
        switch (type) {
          case 'list':
            if (!!list && !!(list.dataList) && !!(list.dataList.length)) {
              let patientListTotals = (patientListTotal / 2 | 0) * 2
              let flattenArr = list.dataList.slice(0, patientListTotals);
              if (!!flattenArr && (flattenArr.length) >= 20) {
                return this.$lodash.chunk(flattenArr, patientListTotals / 2 | 0)
              } else {
                let arr = (new Array(20 - flattenArr.length).fill({}));
                let flattenArr2 = [...flattenArr, ...arr];
                return this.$lodash.chunk(flattenArr2, 20 / 2 | 0)
              }
            }
            break;
          case 'total':
            if (list?.dataTotal?.length) {
              let dataTotal = list['dataTotal'] || Array();
              let findInfo = dataTotal.find((item) => {
                if (item.deptName === info) {
                  return item
                }
              })
              return (findInfo?.total) || 0;
            } else {
              return '';
            }
        }
      }
    }
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 接收父组件传递过来的数据
    async executeMethod(key, data) {
      switch (key) {
        case "publishListInfo":
          let publicListInfo = this.$lodash.cloneDeep(data);
          await this.$store.dispatch('setBoardPatientListInfo', publicListInfo)
          break
      }
    }
  },

  watch: {
    // 获取配置项
    field: {
      handler: function (val) {
        // let comps = this.$lodash.concat(containers, bizFields, customFields, sceneFields);
        // let type = (val.type);
        // const originalComp = this.$lodash.find(comps, {type: type});
        // let originalKey = originalComp['options']['config'];
        // let valKey = val['options']['config'];
        // let originalOptions = originalComp['options'][originalKey];
        // let valOptions = val['options'][valKey];
        // this.optionsInfo = this.$lodash.merge(originalOptions, valOptions);
        let key = (val.options.config);
        this.optionsInfo = (val.options[key]);
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },

};
</script>

<style lang="scss" scoped>
.scene-public-board {
  background-color: transparent;
  color: #333;
  height: 100%;
  line-height: 100%;
  display: flex;
  justify-content: center;
  flex-direction: column;
  padding: 14px 20px;
  box-sizing: border-box;

  .board-wrap {
    height: calc(100% - 54px);

    .board-inside {
      height: 100%;

      .flex-item {
        overflow: hidden;
        border-radius: 12px;
        border: none;
        display: flex;
        width: 100%;
        flex-direction: column;

        .header-area {
          display: flex;
          color: #EBEEF5;
          padding: 14px 0;
          width: 100%;
          background: #0059B3;

          div.header-item {
            flex: 1;
            white-space: nowrap;
            line-height: 36px;
            text-align: center;
          }
        }

        .list-area {
          background: white;
          display: flex;
          flex-direction: column;
          flex-grow: 1;
          width: 100%;

          .list-info {
            display: flex;
            flex-direction: row;
            flex-grow: 1;
            overflow: hidden;
            width: 100%;
            border-top: 6px #E5EAEF solid;

            &:first-child {
              border-top: none;
            }

            div.list-item {
              font-size: 32px;
              overflow: hidden;
              text-align: center;
              padding: 14px 0;
              display: flex;
              justify-content: center;
              align-self: center;
              align-items: center;
              flex: 1;
              color: #333333;
              flex-wrap: nowrap;
              white-space: nowrap;
              @include singleEllipsis(100%);
              height: 42px;
              position: relative;
              min-width: 30px;

              .number {
                border-radius: 12px;
                overflow: hidden;
                padding: 4px 32px;
                background: rgba(0, 89, 179, 0.2);
              }

              .special {
                color: #F56C6C;
              }

              .sign {
                position: relative;
                top: -12px;
                font-size: 42px;
                color: #F05050;
              }

              .color-svg-icon {
                font-size: 36px;
                color: #E6A23C;
                font-weight: bolder;
                position: relative;
                top: -10px;
              }
            }
          }
        }
      }

      .flex-box-left {
        margin-right: 10px;

      }

      .flex-box-right {
        margin-left: 10px;
      }
    }

  }

  .total-area {
    display: flex;
    width: 100%;
    margin-top: 14px;
    flex-direction: row;
    align-self: center;
    flex-basis: 54px;
    line-height: 54px;
    overflow: hidden;

    .total-item {
      display: flex;
      font-size: 38px;
      font-weight: 560;
      margin-right: 60px;
      position: relative;

      .sign {
        position: relative;
        top: -12px;
        font-size: 42px;
        color: #F05050;
      }

      .el-icon-star-on {
        position: relative;
        top: 2px;
        font-size: 52px;
        color: #F05050;
      }

      .color-svg-icon {
        font-size: 40px;
        color: #E6A23C;
        font-weight: bolder;
        position: relative;
        top: 6px;
      }
    }
  }

}

</style>